package com.example.composableexample.grid

import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composableexample.R

@Composable
fun GridSimple() {
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        items(50) {
            Text(text = "分组 $it")
            LazyVerticalGrid(
                columns = GridCells.Fixed(3), modifier = Modifier
                    .height(200.dp)
                    .fillMaxWidth()
            ) {
                items(100) {
                    Image(
                        painter = painterResource(id = R.drawable.jetpack),
                        contentDescription = "",
                        modifier = Modifier.size(50.dp)
                    )
                }
            }
        }
    }
}


@Preview
@Composable
fun LazyVerticalGridSimple() {
    val sections = (0 until 50).toList()
    LazyVerticalGrid(
        columns = GridCells.Fixed(3),
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        sections.forEachIndexed { index, items ->
            item(span = { GridItemSpan(maxLineSpan) }) {
                Text(text = "This is section $index")
            }
            items(items, span = { GridItemSpan(1) }) {
                Text(
                    text = "Item $it",
                    Modifier
                        .border(1.dp, Color.Blue)
                        .height(80.dp)
                        .wrapContentSize()
                )
            }
        }
    }
}